<?php
//生成自定义菜单
$appid = "wx14ccb4d860ebe19c";
$appsecret = "dd8db548c89b1fa3cea051e602cace2f";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
//get请求生成accesstoken
$access_token = gettoken($url);
$jsonmenu = '{
    "button": [
        {
            "type": "view", 
            "name": "通知公告", 
            "url": "http://wx.joygov.com/wechat/wx.php?action=dialog"
        },
        {
            "name": "乐办事",
            "sub_button": [				
				{ 
				   "type": "click", 
				   "name": "办事指南", 
				   "key": "正在开发，敬请期待…"
			    }, 
				{ 
				   "type": "click", 
				   "name": "在线查询", 
				   "key": "正在开发，敬请期待…"
			    }                				              
            ]
        },
        {
            "name": "我的乐政", 
            "sub_button": [
				{
                    "type": "view", 
                    "name": "了解乐政", 
                    "url": "http://mp.weixin.qq.com/s/VPOfZOFhOEWC8whGHE-DbQ"
                },
				{
                    "type": "view", 
                    "name": "APP下载", 
                    "url": "http://wx.joygov.com/index.php/home/appdown/index"
                }, 
				{
                    "type": "view", 
                    "name": "滴滴红包", 
                    "url": "https://dc.tt/1vpIc"
                }
            ]
        }	
    ]
}';


$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
//post请求生成菜单

$result = https_request($url, $jsonmenu);

function https_request($url, $data = null)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $data = curl_exec($ch);
    curl_close($ch);
    print_r($data);

}

function gettoken($url)
{
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 1);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    $response = json_decode($data);
    return $response->access_token;
}

	
	
